<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="员工姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入员工姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <div class="block">
        <span class="demonstration">指定出勤日期</span>
        <el-date-picker
          style="margin-left:5px;margin-right: 5px"
          v-model="queryParams.date"
          type="month"
          value-format="yyyy-MM"
          placeholder="选择月">
        </el-date-picker>
      </div>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:attendance:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="attendanceList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="员工姓名" align="center" prop="name" />
      <el-table-column label="部门" align="center" prop="deptName" width="100"/>
      <el-table-column label="职位" align="center" prop="position" />
      <el-table-column label="应出勤天数" align="center" prop="attenDays" />
      <el-table-column label="实际出勤天数" align="center" prop="attenSdays" width="100"/>
      <el-table-column label="工作时长(单位小时)" align="center" prop="workTime" />
      <el-table-column label="休息天数" align="center" prop="restDays" />
      <el-table-column label="迟到次数" align="center" prop="lateCount" />
      <el-table-column label="早退次数" align="center" prop="leaveEarly" />
      <el-table-column label="旷工天数" align="center" prop="absenteeismDays" />
      <el-table-column label="日期" align="center" prop="date" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.date, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="120" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:attendance:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:attendance:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改出勤汇总表对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="员工姓名" prop="eno" >
          <el-input v-model="form.name" :disabled="true" placeholder="请输入员工姓名" />
        </el-form-item>
        <el-form-item label="部门" prop="deptName">
          <el-input v-model="form.deptName" :disabled="true" placeholder="请输入部门" />
        </el-form-item>
        <el-form-item label="职位" prop="position">
          <el-input v-model="form.position" :disabled="true" placeholder="请输入职位" />
        </el-form-item>
        <el-form-item label="应出勤天数" prop="attenDays">
          <el-input-number v-model="form.attenDays" size="small" :min="15" :max="30" label="应出勤天数"></el-input-number>&nbsp;&nbsp;天
        </el-form-item>
        <el-form-item label="实际出勤天数" prop="attenSdays">
          <el-input-number v-model="form.attenSdays" size="small" :min="0" :max="30" label="实际出勤天数"></el-input-number>&nbsp;&nbsp;天
        </el-form-item>
        <el-form-item label="休息天数" prop="restDays">
          <el-input-number v-model="form.restDays" size="small" :min="0" :max="30" label="休息天数"></el-input-number>&nbsp;&nbsp;天
        </el-form-item>
        <el-form-item label="迟到次数" prop="lateCount">
          <el-input-number v-model="form.lateCount" size="small" :min="0" :max="30" label="迟到次数"></el-input-number>&nbsp;&nbsp;次
        </el-form-item>
        <el-form-item label="早退次数" prop="leaveEarly">
          <el-input-number v-model="form.leaveEarly" size="small" :min="0" :max="30" label="早退次数"></el-input-number>&nbsp;&nbsp;次
        </el-form-item>
        <el-form-item label="旷工天数" prop="absenteeismDays">
          <el-input-number v-model="form.absenteeismDays" size="small" :min="0" :max="30" label="旷工天数"></el-input-number>&nbsp;&nbsp;天
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAttendance, getAttendance, delAttendance, addAttendance, updateAttendance } from "@/api/system/attendance";

export default {
  name: "Attendance",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 出勤汇总表表格数据
      attendanceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        date: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询出勤汇总表列表 */
    getList() {
      this.loading = true;
      console.log(this.queryParams)
      listAttendance(this.queryParams).then(response => {
        this.attendanceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        eno: null,
        deptId: null,
        postId: null,
        attenDays: null,
        attenSdays: null,
        workTime: null,
        restDays: null,
        lateCount: null,
        leaveEarly: null,
        absenteeismDays: null,
        date: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加出勤汇总表";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      console.log(row)
      this.reset();
      const id = row.id || this.ids
      getAttendance(id).then(response => {
        this.form = response.data;
        this.form.name = row.name;
        this.form.deptName = row.deptName;
        this.form.position = row.position;
        this.open = true;
        this.title = "修改出勤汇总表";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateAttendance(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAttendance(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除出勤汇总表编号为"' + ids + '"的数据项？').then(function() {
        return delAttendance(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/attendance/export', {
        ...this.queryParams
      }, `attendance_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
